﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Autorun
{
    public partial class PaneServer : Autorun.PaneBase
    {
        private const string ICO_REFRESH = "icoRefresh";

        #region "Constructors & overriden methods"
        public PaneServer()
        {
            InitializeComponent();
        }

        public override bool Initialize(IPresenter sender, ShowPaneEventArgs e)
        {
            _parent = sender;

            if ((e != null) && (e.Args != null) && (e.Args.Length != 0))
                if (e.Args[0] is ServerManagement)
                    _proxy = (ServerManagement)e.Args[0];
                else
                    _proxy = null;
            else
                _proxy = null;

            LoadIcons();
            mnuRefresh.Image = myImages.Images[ICO_REFRESH];
            RefreshInformation();
            RefreshServerInformation();

            return true;
        }

        public override void Terminate()
        {
            if (_proxy != null)
            {
                try
                {
                    _proxy.Service.OnNotify -= Service_OnNotify;
                }
                catch  { }
                //myProperty.SelectedObject = null;
            }
            base.Terminate();
        }
        #endregion

        #region "Methods"
        private void RefreshServerInformation()
        {
            if (_proxy == null)
            {
                Caption = "Not connected";
                lblServer.Text = null;
                lblVersion.Text = null;
                lblDirectory.Text = null;
                lblDatabase.Text = null;
                lblDomain.Text = null;
            }
            else
            {
                Caption = string.Format("{0}: Server Configuration", _proxy.Information.MachineName);

                try
                {
                    _proxy.Service.OnNotify += new NotifyEventHandler(Service_OnNotify);
                }
                catch  { }

                lblServer.Text = _proxy.Information.MachineName;
                lblVersion.Text = _proxy.Information.Version.ToString();
                lblDirectory.Text = _proxy.Information.HomeDirectory;
                lblDatabase.Text = _proxy.Information.Database;
                lblDomain.Text = _proxy.Information.Domain;

                RefreshInformation();
            }
        }

        private void LoadIcons()
        {
            myImages.Images.Clear();
            myImages.Images.Add(ICO_REFRESH, Properties.Resources.icoRefresh);
        }

        private void RefreshInformation()
        {
            if (_proxy != null)
            {
                myProperty.SelectedObject = _proxy.Configuration;
            }
        }
        #endregion

        #region "Controls Events Handlers"
        private void mnuRefresh_Click(object sender, EventArgs e)
        {
            RefreshInformation();
        }
        #endregion

        #region "Server Events Handlers"
        void Service_OnNotify(object sender, NotifyEventArgs e)
        {
            RefreshInformation();
        }
        #endregion

    }
}
